/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tuwien.big.memory.entities;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import tuwien.big.memory.controller.MemoryControl;

/**
 *
 * @author Dieter
 */
@ManagedBean(name="playerPool")
@ApplicationScoped
public class PlayerPool {
    private Hashtable<String,Player> players;
    private Hashtable<Player,MemoryControl> memcons;
    private MemoryControl gm = null;

    /** Creates a new instance of PlayerPool */
    public PlayerPool() {
        players = new Hashtable();
        memcons = new Hashtable<Player, MemoryControl>();
    }

    public MemoryControl addPlayer(Player p)
    {
        if(!players.containsKey(p.getName()))
        {
           players.put(p.getName(),p);
           if(gm == null)
           {
               gm = new MemoryControl(p,null);
               memcons.put(p, gm);
               return gm;
           }
           else
           {
                memcons.put(p, gm);
                MemoryControl gmready = gm;
                gm = null;
                gmready.setPlayer2(p);
                gmready.setReadytoplay(true);
                gmready.init();

                System.out.println("ready!!!!!!!!!!!!!!!!!!!!");
                return gmready;
           }
        }

        return memcons.get(p);
    }

    /**
     * @return the count
     */
    public int getCount() {
        return getPlayers().size();
    }

    /**
     * @return the players
     */
    public List<Player> getPlayers() {
        return new ArrayList<Player>(players.values());
    }
}
